package cn.signalpha.modules.pay.provider;

import cn.signalpha.core.secure.exception.SecureException;
import cn.signalpha.core.tool.utils.SpringUtil;
import cn.signalpha.modules.pay.enums.PayTypeEnum;
import cn.signalpha.modules.pay.granter.*;
import lombok.AllArgsConstructor;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * PayGranterBuilder
 */
@AllArgsConstructor
public class PayGranterBuilder {

	/**
	 * TokenGranter缓存池
	 */
	private static final Map<String, IPayGranter> GRANTER_POOL = new ConcurrentHashMap<>();

	static {
		GRANTER_POOL.put(PayTypeEnum.WX_PAY.getType(), SpringUtil.getBean(WxPayGranter.class));
		GRANTER_POOL.put(PayTypeEnum.WX_PAY_V3.getType(), SpringUtil.getBean(WxPayV3Granter.class));
		GRANTER_POOL.put(PayTypeEnum.WX_PAY_V3_HD.getType(), SpringUtil.getBean(WxPayV3HDGranter.class));
		GRANTER_POOL.put(PayTypeEnum.ALI_PAY.getType(), SpringUtil.getBean(AliPayGranter.class));
		GRANTER_POOL.put(PayTypeEnum.JOIN_PAY_WX.getType(), SpringUtil.getBean(JoinPayGranter.class));
		GRANTER_POOL.put(PayTypeEnum.JOIN_PAY_ALI.getType(), SpringUtil.getBean(JoinPayGranter.class));
		GRANTER_POOL.put(PayTypeEnum.JOIN_PAY_FAST.getType(), SpringUtil.getBean(JoinPayFastGranter.class));
	}

	/**
	 * 获取PayGranter
	 */
	public static IPayGranter getGranter(String payType) {
		IPayGranter payGranter = GRANTER_POOL.get(payType);
		if(payGranter == null){
			throw new SecureException("支付类型错误");
		}
		return payGranter;
	}

}
